<?php
namespace catchAdmin\externalGoods\model;

use catchAdmin\externalGoods\model\search\NotifySearch;
use catcher\base\CatchModel;
use think\Exception;
use think\facade\Db;

class Notify extends CatchModel
{

    use NotifySearch;

    protected $name = 'db_syst_sync_notify';
    protected $tablePrefix = 'db_syst_sync_notify_';
    protected $defaultSoftDelete = null;
    protected $field = [
        'id',
        'type',
        'type_name',
        'status',
        'notify_data',
        'remark',
        'sign',
        'supplier_id',
        'created_at',
        'updated_at',
        'deleted_at',
        'tenant_id',
        'creator_id'
    ];

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->setTableName($this->getTableName());
    }

    public function getTableName()
    {
        $currentMonth = date('Y_m');
        return $this->tablePrefix . $currentMonth;
    }

    // 动态设置数据表名
    public function setTableName($tableName)
    {
        $this->name = $tableName;
        return $this;
    }

    public function getList()
    {
        $where = [];

        return $this->withoutField('updated_at,deleted_at,supplier_id,tenant_id,creator_id')
            ->catchSearch()
            ->where($where)
            ->order($this->aliasField('id'), 'desc')
            ->paginate()
            ->each(function (&$item) {
                $item['notify_data'] = json_decode($item['notify_data'],true);
            });
    }
}
